//?var jq = jQuery.noConflict();
/**
 * 增删改工具栏
 */
window.onerror = function() {
	return true;
};
var iframe;// iframe操作对象
var win;//窗口对象
var gridname="";//操作datagrid对象名称
var windowapi ;
var currentWin;
var reloadList=null;

/**
 * 添加事件打开窗口
 * @param title 编辑框标题
 * @param addurl//目标页面地址
 */
function add(title,addurl,gname,width,height) {
	var width = width?width:top.document.body.offsetWidth;
	var height = height?height:top.document.body.offsetHeight-80;
	gridname=gname;
	createwindow(title, addurl,width,height);
}
/**
 * 如果页面是详细查看页面，无效化所有表单元素，只能进行查看
 */
$(function(){
	if(location.href.indexOf("load=detail")!=-1){
		$(":input").attr("disabled","true");
		$("span[class='Validform_checktip']").hide();
	}
});

/**
 * 查看时的弹出窗口
 * 
 * @param title
 * @param addurl
 * @param saveurl
 */
function createdetailwindow(title, addurl,width,height,windowapi) {
	width = width?width:700;
	height = height?height:400;
	if(width=="100%" || height=="100%"){
		width = document.body.offsetWidth;
		height =document.body.offsetHeight-100;
	}
	if(typeof(windowapi) == 'undefined'){
		$.dialog({
			content: 'url:'+addurl,
			lock : true,
			width:width,
			height: height,
			title:title,
			opacity : 0.3,
			cache:false, 
		    cancelVal: '关闭',
		    cancel: true /*为true等价于function(){}*/
		});
	}else{
		windowapi.opener.$.dialog({
			content: 'url:'+addurl,
			lock : true,
			width:width,
			height: height,
			parent:windowapi,
			title:title,
			opacity : 0.3,
			cache:false
		});
	}
	
}
// 普通询问操作调用函数
function lhConfirm(content,func,cancFunc) {
	$.dialog.confirm(content, function(){
		if(func){
			func();
		}
		return true;
	},function(){
		if(cancFunc){
			cancFunc();
		}
	});
	
};

function tip(msg,type,time){
	var icon='/tips.gif';
	if(type=='success'){
		icon='/success.gif';
	}else if(type=='requestion'){
		icon='/prompt.gif';
	} else if(type=='error') {
		icon='/error.gif';
	}
	if(!time){
		time=(msg.length*0.15);
		if(time<1.5){
			time=1.5;
		}else if(time>5){
			time=5;			
		}
	}
	var tipWin=top.$.dialog.tips(msg,time,icon);
	tipWin.zindex();
	tipWin.zindex();
	tipWin.zindex();
}
/**
弹出
*/
function alert(content,callback){
	var tipWin=top.$.dialog.alert(content,callback);
	tipWin.zindex();
}

/**
 * 更新事件打开窗口
 * @param title 编辑框标题
 * @param addurl//目标页面地址
 * @param id//主键字段
 */
function update(title,url, id,idName,width,height,func) {
	gridname=id;
	var rowsData = $('#'+id).datagrid('getSelections');
	if (!rowsData || rowsData.length==0) {
		tip('请选择编辑项目');
		return;
	}
	if (rowsData.length>1) {
		tip('请选择一条记录再编辑');
		return;
	}
	url += '&'+idName+'='+rowsData[0][idName];
	createwindow(title,url,width,height,func);
}

/**
 * 创建添加或编辑窗口
 * @param title
 * @param addurl
 * @param saveurl
 */
function createwindow(title, addurl,width,height,func,param) {
	width = width?width:700;
	height = height?height:400;
	if(width=="100%" || height=="100%"){
		width = document.body.offsetWidth;
		height =document.body.offsetHeight-100;
	}
	if(typeof(windowapi) == 'undefined'){
		$.dialog({
			content: 'url:'+addurl,
			lock : true,
			width:width,
			height:height,
			title:title,
			opacity : 0.3,
			cache:false,
		    ok: function(){
		    	iframe = this.iframe.contentWindow;
		    	if(func){
					func(param);
		    	}else{
		    		saveObj();
		    	}
				return false;
		    },
		    cancelVal: '关闭',
		    cancel: true /*为true等价于function(){}*/
		});
	}else{
		windowapi.opener.$.dialog({
			content: 'url:'+addurl,
			lock : true,
			width:width,
			height:height,
			parent:windowapi,
			title:title,
			opacity : 0.3,
			cache:false,
		    ok: function(){
		    	iframe = this.iframe.contentWindow;
				saveObj();
				return false;
		    },
		    cancelVal: '关闭',
		    cancel: true /*为true等价于function(){}*/
		});
	}
	
}


/**
 * 重新加载附件列表
 * @return
 */
function reload() {
}


/**
 * 创建不带按钮的窗口
 * 
 * @param title
 * @param addurl
 * @param saveurl
 */
function openwindow(url,width,height,okfunc,cancFunc) {
	var width = width?width:top.document.body.offsetWidth;
	var height = height?height:top.document.body.offsetHeight-80;
	var dialog_win=$.dialog({
		content: 'url:'+url,
		title :'',
		id:'da',
		skin:'blue',
		cache:false,
		lock : true,
		min : false,
		max : false,
		width:width,
		height:height,
		okVal:'确定',
		cancelVal: '关闭',
		cancel: true,
		parent:window.parent,
		ok: function() {
			iframe = this.iframe.contentWindow;
			if(okfunc){
	    		okfunc();
				return false;
	    	}else{
	    		return true;
	    	}
		},
		close: function(){
			iframe = this.iframe.contentWindow;
			if(cancFunc){
	    		cancFunc();
	    	}else if(reloadList){
	    		reloadList();
	    	}
	    }
	});
}
function openFullWindow(title,url,okfunc,cancFunc) {
	var dialog_win=$.dialog({
		content: 'url:'+url,
		title :title,
		skin:'blue',
		cache:false,
		lock : true,
		min : true,
		max : true,
		width:'100%',
		height:'100%',
		cancelVal: '关闭',
		cancel: true,
		parent:window.parent,
		close: function(){
			iframe = this.iframe.contentWindow;
			if(cancFunc){
	    		cancFunc();
	    	}
	    },
	    okVal:'确定',
	    ok: function(){
	    	iframe = this.iframe.contentWindow;
	    	if(okfunc){
	    		return okfunc();
	    	}
			return false;
		}
	});
	dialog_win.max();
}
function openWindowWithTitle(title,url,cancFunc) {
	var width = width?width:top.document.body.offsetWidth;
	var height = height?height:top.document.body.offsetHeight-65;
	var dialog_win=$.dialog({
		content: 'url:'+url,
		title :title,
		id:'da',
		skin:'blue',
		cache:false,
		lock : true,
		min : false,
		max : false,
		width:width,
		height:height,
		cancelVal: '关闭',
		cancel: true,
		parent:window.parent,
		close: function(){
			iframe = this.iframe.contentWindow;
			if(cancFunc){
	    		cancFunc();
	    	}
	    }
	});
}
function choseConfirm(title,url,width,height,okFunc,cancFunc) {
	var width = width?width:300;
	var height = height?height:200;
	var dialog_win=$.dialog({
		content: 'url:'+url,
		title :title,
		id:'chose',
		skin:'blue',
		cache:false,
		lock : true,
		min : false,
		max : false,
		width:width,
		height:height,
		cancelVal: '关闭',
		cancel: true,
		parent:window.parent,
		ok:function(){
			iframe = this.iframe.contentWindow;
			if(okFunc){
				okFunc();
				return false;
			}else{
				return true;
			}
		},
		close: function(){
			iframe = this.iframe.contentWindow;
			if(cancFunc){
	    		cancFunc();
	    	}else if(reloadList){
	    		reloadList();
	    	}
	    }
	});
}

function isChecked(oThis){
    $(oThis).parent().addClass("ui_radio_checked");
    $(oThis).parent().siblings().removeClass("ui_radio_checked")
}


function top_win(){
	try{
	   	var  _doc = window.top.document;  // 跨域|无权限
		_doc.getElementsByTagName; // chrome 浏览器本地安全限制
		return window
	}catch(e){
	     return window;
	};
}


/**
 * 执行保存
 * 
 * @param url
 * @param gridname
 */
function saveObj() {
	$('#btn_sub', iframe.document).click();
}


/**
 * 执行AJAX提交FORM
 * 
 * @param url
 * @param gridname
 */
function ajaxSubForm(url) {
	$('#myform', iframe.document).form('submit', {
		url : url,
		onSubmit : function() {
			iframe.editor.sync();
		},
		success : function(r) {
			tip('操作成功');
		}
	});
}

/**
 * 退出确认框
 * 
 * @param url
 * @param content
 * @param index
 */
function exit(url, content) {
	$.dialog.confirm(content, function(){
		window.location = url;
	}, function(){
	});
}

/**
 * ajax提交FORM
 * 
 * @param url
 * @param gridname
 */
function ajaxdoForm(formname) {
	$('#' + formname).form('submit', {
		onSubmit : function() {
		},
		success : function(r) {
			tip('操作成功');
		}
	});
}
//删除记录
function deleteFunc(url,data,func,param){
   $.dialog.confirm('你确定此操作吗?', function(){
     $.post(url,data,function(data){
        var d = $.parseJSON(data);
	    if (d.success) {
			var msg = d.msg;
			tip(msg);
			if(func){
				func(param);
			}
		}else{
			var msg = d.msg;
			tip(msg);
		}
     });
   }, function(){
         tip('执行取消操作');
   });
};

requestWithTip=function(url,jsonData){
	 $.ajax({
	   url: url,
	   type:"post",
	   processData: true,
	   contentType:'application/x-www-form-urlencoded',
	   data: jsonData,
	   success: function(resultData){
		   var d = $.parseJSON(resultData);
			if (d.success) {
				var msg = d.msg;
				tip(msg);
			}else{
				var msg = d.msg;
				tip(msg);
			}
	   },
	   error:function(){
	   		alert('请求失败');
	   }
	 });
	
};

requestWithFunc=function(url,data,func){
	 $.ajax({
	   url: url,
	   type: "POST",
	   processData: false,
	   contentType: "application/x-www-form-urlencoded; charset=GBK",
	   data: data,
	   success:function(data) {
			var d = $.parseJSON(data);
			if (d.success) {
				var msg = d.msg;
				if(func){
					func();
				}
			}else{
				var msg = d.msg;
				tip(msg);
			}
		},
		error:function(){
	   		alert('请求失败');
	   }
	});
};
//创建没有按钮的窗口
function withOutBtnwindow(title,url,width,height) {
	var width = width?width:top.document.body.offsetWidth;
	var height = height?height:top.document.body.offsetHeight-80;
	title=title||"";
	var dialog_win=$.dialog({
		content: 'url:'+url,
		title :title,
		skin:'blue',
		cache:false,
		lock : true,
		min : true,
		max : true,
		width:width,
		height:height,
		cancelVal: '关闭',
		cancel: true
	});
}


//数组转换为json格式数据
function arrayToJson(values){
	var data="{"+values.join(',')+"}";
	return eval("("+data+")");
}

function escapeHTML(html){
	  return String(html)
	    .replace(/&(?!\w+;)/g, '&amp;')
	    .replace(/</g, '&lt;')
	    .replace(/>/g, '&gt;')
	    .replace(/"/g, '&quot;');
};
